/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dorian.atomicuml.dao;

import com.dorian.atomicuml.dao.interfaces.DiagrammsManager;
import com.dorian.atomicuml.orm.Diagramms;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author cedric.creusot
 */
public class DiagrammsManagerImpl implements DiagrammsManager {
    
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    @Override
    public int createDiagramms(String name, String description, Integer editorId) {
        Session session = sessionFactory.getCurrentSession();
        
        Diagramms d = new Diagramms(0, name, description, editorId);
        
        try
        {
            session.save(d);
            return d.getId();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public boolean deleteDiagramms(int id) {
        Session session = sessionFactory.getCurrentSession();
        
        try
        {
            Diagramms d = getDiagramms(id);
            session.delete(d);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteDiagramms(Diagramms diagramms) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Diagramms getDiagramms(int id) {
        Session session = sessionFactory.getCurrentSession();
        
        Query query = session.getNamedQuery("Diagramms.findById")
                             .setParameter("id", id);
        return (Diagramms)query.uniqueResult();
    }

    @Override
    public Diagramms getDiagramms(int id, String name) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean updateDiagramms(Diagramms diagramms) {
        Session session = sessionFactory.getCurrentSession();
        
        try
        {
            session.saveOrUpdate(diagramms);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }
    
}
